home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Apple Color OneScanner SDK / Scan Image 1.0 / Source / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-24  |  12.3 KB  |  617 lines  |  [TEXT/MPCC]

  1. /*************************************************************************************
  2. #
  3. #        events.c
  4. #
  5. #        This segment handles the basic event calls.
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            4/3/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Events.h>
  29. #include <ToolUtils.h>
  30. #include <Gestalt.h>
  31. #include <OSUtils.h>
  32. #include <Palettes.h>
  33.  
  34. #include "App.h"
  35. #include "Proto.h"
  36.  
  37. //----------------------------------------------------------------------
  38. //    Globals
  39. //----------------------------------------------------------------------
  40.  
  41. extern Boolean        gInBackground;
  42. extern Boolean        gDone;
  43. extern Boolean        gHasAbout;        // have an about box?
  44.  
  45.  
  46. //----------------------------------------------------------------------
  47. //
  48. //    EventLoop - main entry and loop for all event processing
  49. //
  50. //
  51. //----------------------------------------------------------------------
  52.  
  53. void EventLoop(void)
  54. {
  55.     EventRecord        event;
  56.     RgnHandle        cursorRgn;
  57.     Boolean            gotEvent;
  58.         
  59.     gDone = false;
  60.     cursorRgn = NewRgn();
  61.         
  62.     do 
  63.     {
  64.         gotEvent = WaitNextEvent(everyEvent,&event,MyGetSleep(),cursorRgn);
  65.                                     
  66.         if (gotEvent)
  67.             DoEvent(&event);
  68.  
  69.                         
  70.     } while ( !gDone );
  71.         
  72.     DisposeRgn(cursorRgn);
  73. }
  74.  
  75.  
  76. //----------------------------------------------------------------------
  77. //
  78. //    MyGetSleep - return sleep value based upon whether or not the app
  79. //                 is in the background.
  80. //
  81. //----------------------------------------------------------------------
  82.  
  83. short MyGetSleep(void)
  84. {
  85.     short        sleep = 30;
  86.     
  87.     if (gInBackground)
  88.         sleep = 1L;
  89.  
  90.     return sleep;
  91.  
  92. }
  93.  
  94.  
  95. //----------------------------------------------------------------------
  96. //
  97. //    CustomWindowEvent - Handles custom procs assigned to a window. 
  98. //                        Different window kinds can easily have unique event
  99. //                        handlers, ie. floaters, dialogs, documentprocs
  100. //----------------------------------------------------------------------
  101.  
  102. void CustomWindowEvent(short eventType,WindowRef window,void *refCon)
  103. {
  104.     CustomProc        theProc;
  105.     short            kind;
  106.     DocHnd            doc;
  107.     
  108.     kind = GetWindKind(window);
  109.     if (kind < kDocKind || kind > kAboutKind)     // not our window
  110.         return;
  111.  
  112.     doc = (DocHnd)GetWRefCon(window);
  113.     
  114.     if (doc != nil) 
  115.     {
  116.         HLock((Handle)doc);
  117.         switch(eventType) 
  118.         {
  119.             case kIdleProc:
  120.                 theProc = (**doc).idleProc;
  121.                 break;
  122.                 
  123.             case kMenuProc:
  124.                 theProc = (**doc).mMenuProc;
  125.                 break;
  126.  
  127.             case kInContentProc:
  128.                 theProc = (**doc).inContentProc;
  129.                 break;
  130.  
  131.             case kInGoAwayProc:
  132.                 theProc = (**doc).inGoAwayProc;
  133.                 break;
  134.                 
  135.             case kInZoomProc:
  136.                 theProc = (**doc).inZoomProc;
  137.                 break;
  138.                 
  139.             case kInGrowProc:
  140.                 theProc = (**doc).inGrowProc;
  141.                 break;
  142.                 
  143.             case kMUpProc:
  144.                 theProc = (**doc).mUpProc;
  145.                 break;
  146.                 
  147.             case kKeyProc:
  148.                 theProc = (**doc).keyProc;
  149.                 break;
  150.             
  151.             case kActivateProc:
  152.                 theProc = (**doc).activateProc;
  153.                 break;
  154.  
  155.             case kUpdateProc:
  156.                 theProc = (**doc).updateProc;
  157.                 break;
  158.  
  159.             default:
  160.                 theProc = nil;
  161.                 break;
  162.         }        
  163.     
  164.         if (theProc != nil) 
  165.         {            
  166.             (*theProc)(window,refCon);
  167.             HUnlock((Handle)doc);
  168.     
  169.         }
  170.     }    
  171.     
  172.     
  173. }
  174.  
  175.  
  176. //----------------------------------------------------------------------
  177. //
  178. //    DoEvent - event dispatcher, called by eventloop
  179. //                
  180. //
  181. //----------------------------------------------------------------------
  182.  
  183. void DoEvent(EventRecord *event)
  184. {
  185.     OSErr            err;
  186.     short            kind;
  187.     long            menuChoice;
  188.     Point            thePoint;
  189.     Boolean            active;
  190.     WindowRef        window;
  191.     
  192.     window = FrontWindow();
  193.  
  194.     switch(event->what) 
  195.     {
  196.         case nullEvent:
  197.             CustomWindowEvent(kIdleProc, window, nil);
  198.             break;
  199.             
  200.         case mouseDown:
  201.             HandleMouseDown(event);
  202.             break;
  203.                             
  204.         case mouseUp:
  205.             break;
  206.                             
  207.         case keyDown:
  208.         case autoKey:
  209.             if (event->modifiers & cmdKey)          //    is cmd key down
  210.             {
  211.                 menuChoice = MenuKey(event->message & charCodeMask);
  212.                 kind = GetWindKind(window);
  213.                 if (kind < kDocKind || kind > kFloatKind)             // not our window
  214.                     HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  215.                 else    
  216.                     CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  217.             }
  218.             break;
  219.                             
  220.         case activateEvt:
  221.             gInBackground = event->modifiers & activeFlag;
  222.             active = gInBackground;
  223.             CustomWindowEvent(kActivateProc, (WindowRef)event->message, &active);
  224.             break;
  225.                             
  226.         case updateEvt:
  227.             UpdateWindow((WindowRef)event->message);
  228.             break;
  229.                             
  230.         case diskEvt:
  231.             if (HiWord(event->message) != noErr) 
  232.             {
  233.                 SetPt(&thePoint, 50, 50);
  234.                 err = DIBadMount(thePoint, event->message);
  235.             }
  236.             break;
  237.                             
  238.         case osEvt:
  239.             switch ((event->message >> 24) & 0x0FF) 
  240.             {        
  241.                 case suspendResumeMessage:    
  242.                     gInBackground = event->message & resumeFlag;
  243.                     active = gInBackground;
  244.                     CustomWindowEvent(kActivateProc, FrontWindow(), &active);
  245.                     break;
  246.             }
  247.             break;
  248.     
  249.         case kHighLevelEvent:
  250.             AEProcessAppleEvent(event);
  251.             break;
  252.     }
  253.  
  254. }            
  255.  
  256.  
  257.  
  258. //----------------------------------------------------------------------
  259. //
  260. //    DoIdle - handle Idle events
  261. //                
  262. //
  263. //----------------------------------------------------------------------
  264.  
  265. void DoIdle(WindowRef window, void *refCon)
  266. {
  267.  
  268. }
  269.  
  270.  
  271. //----------------------------------------------------------------------
  272. //
  273. //    HandleMouseDown - 
  274. //                
  275. //
  276. //----------------------------------------------------------------------
  277.  
  278. void HandleMouseDown(EventRecord *event)
  279. {
  280.     long            menuChoice;
  281.     short            thePart;
  282.     short            kind;
  283.     WindowRef        window;
  284.         
  285.  
  286.     thePart = FindWindow(event->where, &window);
  287.         
  288.     switch(thePart) 
  289.     {
  290.         case inMenuBar:
  291.             menuChoice = MenuSelect(event->where);
  292.             window = FrontWindow();
  293.             kind = GetWindKind(window);
  294.             if (kind < kDocKind || kind > kAboutKind)             // not our window
  295.                 HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  296.             else    
  297.                 CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  298.             break;
  299.  
  300.         case inContent:
  301.             if (window != FrontWindow() && GetWindKind(window) != kFloatKind)
  302.                 SelectWindow(window);
  303.             else
  304.                 CustomWindowEvent(kInContentProc, window, &event->where);
  305.     
  306.             break;
  307.  
  308.         case inSysWindow:
  309.             SystemClick(event,window);
  310.             break;
  311.                                                 
  312.         case inDrag:
  313.             if (window != FrontWindow())
  314.                 SelectWindow(window);
  315.             DragWindow(window, event->where,&qd.screenBits.bounds);
  316.             break;
  317.                         
  318.         case inGoAway:
  319.             if (TrackGoAway(window, event->where))
  320.                 RemoveWindow(window);
  321.             break;
  322.                         
  323.         case inZoomIn:
  324.         case inZoomOut:
  325.             if (TrackBox(window,event->where,thePart)) 
  326.                 CustomWindowEvent(kInZoomProc, window, &thePart);
  327.             break;
  328.                         
  329.         case inGrow:
  330.             CustomWindowEvent(kInGrowProc, window, &event->where);
  331.             break;
  332.     }
  333.     
  334. }
  335.  
  336.  
  337. //----------------------------------------------------------------------
  338. //
  339. //    HandleMenuChoice - 
  340. //                
  341. //
  342. //----------------------------------------------------------------------
  343.  
  344. void HandleMenuChoice(WindowRef window, void *refCon)
  345. {
  346.     OSErr        err = noErr;
  347.     long         menuChoice;
  348.     short        item, menu;
  349.     short        daRefNum;
  350.     Rect        bounds;
  351.     Str255        daName;
  352.     WindowRef    newWindow;
  353.     
  354.             
  355.     menuChoice = *(long *)refCon;
  356.     
  357.     item = LoWord(menuChoice);
  358.     menu = HiWord(menuChoice);
  359.  
  360.     switch(menu) 
  361.     {
  362.         case mApple:
  363.             switch(item) 
  364.             {
  365.                 case iAbout:
  366.                     if (!gHasAbout) 
  367.                     {
  368.                         SetRect( &bounds,2, 40 ,382 ,162);
  369.                         newWindow = CreateWindow(nil, nil, &bounds, "\pAbout", true,
  370.                                                  documentProc, kAboutKind, (WindowPtr)-1,
  371.                                                  true, nil );
  372.                         gHasAbout = true;
  373.                     }                        
  374.                     break;
  375.                     
  376.                 default:
  377.                     GetItem(GetMHandle(mApple),item,daName);
  378.                     daRefNum = OpenDeskAcc( daName );
  379.                     break;
  380.             }    
  381.             break;
  382.                     
  383.         case mFile:
  384.             switch(item) 
  385.             {
  386.                 case iNew:
  387.                     SetupAndScan();
  388.                     break;
  389.                     
  390.                 case iOpen:
  391.                     DoOpenNew();
  392.                     break;
  393.                         
  394.                 case iClose:
  395.                     RemoveWindow(FrontWindow());
  396.                     break;    
  397.                     
  398.                 case iSave:
  399.                     if (window != nil)
  400.                         err = DoSaveImage(window);
  401.                     break;
  402.  
  403.                 case iQuit:
  404.                     gDone = true;
  405.                     break;
  406.                     
  407.                 default:
  408.                     break;    
  409.             }
  410.             break;
  411.                     
  412.         default:
  413.             break;    
  414.         
  415.     }
  416.     
  417.     HiliteMenu(0);
  418.     
  419. }
  420.  
  421.  
  422. //----------------------------------------------------------------------
  423. //
  424. //    HandleContentClick - 
  425. //                
  426. //
  427. //----------------------------------------------------------------------
  428.  
  429. void HandleContentClick(WindowRef window, void *refCon)
  430. {
  431.     ControlRef            control;
  432.     ControlActionUPP    scrollUPP;
  433.     Point                mouse;
  434.     short                value;
  435.     short                thePart;
  436.     short                oldSetting;
  437.     short                horzScroll,vertScroll;
  438.     DocHnd                doc;
  439.  
  440.     doc = (DocHnd)GetWRefCon(window);
  441.     if (doc != nil) 
  442.     {
  443.         SetPort(window);
  444.         mouse = *(Point *)refCon;
  445.         GlobalToLocal(&mouse);
  446.         horzScroll = vertScroll = 0;
  447.  
  448.         if ((thePart = FindControl(mouse, window, &control)) != 0) 
  449.         {
  450.             switch(thePart) 
  451.             {
  452.                 case inThumb:
  453.                     oldSetting = GetCtlValue(control);
  454.                     thePart = TrackControl(control, mouse, 0L);
  455.                     if (thePart != 0) 
  456.                     {
  457.                         value = oldSetting - GetCtlValue(control);
  458.                         if (value != 0)
  459.                         {
  460.                             if (control == (**doc).hScroll)
  461.                                 horzScroll = value ;
  462.                             if (control == (**doc).vScroll)
  463.                                 vertScroll = value;
  464.                                 
  465.                             MyScrollPicture(window, horzScroll, vertScroll);
  466.                         }
  467.                         
  468.                     }
  469.                     break;
  470.                     
  471.                 case inUpButton:
  472.                 case inDownButton:
  473.                 case inPageUp:
  474.                 case inPageDown:
  475.                     scrollUPP = NewControlActionProc(ScrollActionProc);
  476.                     value = TrackControl(control, mouse, scrollUPP);
  477.                     break;
  478.     
  479.             }
  480.             
  481.         }
  482.         
  483.     }
  484.  
  485.  
  486. }
  487.  
  488.  
  489.  
  490. //----------------------------------------------------------------------
  491. //
  492. //    HandleZoomClick - 
  493. //                
  494. //
  495. //----------------------------------------------------------------------
  496.  
  497. void HandleZoomClick(WindowRef window, void *refCon)
  498. {
  499.     short            part;
  500.     DocHnd            doc;
  501.     
  502.     doc = (DocHnd)GetWRefCon(window);
  503.     if (doc != nil) 
  504.     {
  505.         part = *(short *)refCon;
  506.         SetPort(window);
  507.         
  508.         EraseRect(&window->portRect);
  509.         ZoomWindow(window, part, false);
  510.         InvalRect(&window->portRect);
  511.         
  512.         HideControl((**doc).hScroll);
  513.         HideControl((**doc).vScroll);
  514.         
  515.         AdjustScrollbars(window, true);
  516.         
  517.         ShowControl((**doc).hScroll);
  518.         ShowControl((**doc).vScroll);        
  519.     }
  520. }
  521.  
  522.  
  523. //----------------------------------------------------------------------
  524. //
  525. //    HandleGrow - 
  526. //                
  527. //
  528. //----------------------------------------------------------------------
  529.  
  530. void HandleGrow(WindowRef window, void *refCon)
  531. {
  532.     Rect            gIRect;
  533.     Rect            limitRect;
  534.     long            growSize;
  535.         
  536.     limitRect = (**GetGrayRgn()).rgnBBox;
  537.     limitRect.left += 125;
  538.     limitRect.top += 125;
  539.  
  540.     growSize = GrowWindow(window, *(Point *)refCon, &limitRect);
  541.     if (growSize) 
  542.     {
  543.         SetPort(window);    
  544.         gIRect = window->portRect;
  545.         gIRect.top = gIRect.bottom - kScrollWidth;
  546.         gIRect.left = gIRect.right - kScrollWidth;     
  547.         EraseRect(&gIRect);
  548.         SizeWindow(window, LoWord(growSize), HiWord(growSize),true);
  549.         
  550.         ClipRect(&window->portRect);
  551.         AdjustScrollbars(window, true);
  552.         InvalRect(&window->portRect);
  553.     }
  554.  
  555.  
  556. }
  557.  
  558.  
  559. //----------------------------------------------------------------------
  560. //
  561. //    UpdateWindow - update dispatcher for document windows.
  562. //                 
  563. //
  564. //----------------------------------------------------------------------
  565.  
  566. void UpdateWindow(WindowRef window) 
  567. {
  568.     GrafPtr        oldPort;
  569.     
  570.     
  571.     GetPort(&oldPort);
  572.     
  573.     SetPort(window);
  574.     BeginUpdate(window);
  575.     CustomWindowEvent(kUpdateProc, window, nil);
  576.     EndUpdate(window);
  577.     
  578.     SetPort(oldPort);
  579.  
  580. }
  581.  
  582.  
  583. //----------------------------------------------------------------------
  584. //
  585. //    DoActivate - activate proc for document windows
  586. //                 
  587. //
  588. //----------------------------------------------------------------------
  589.  
  590. void DoActivate(WindowRef window, void *refCon)
  591. {
  592.     Boolean        becomingActive;
  593.     DocHnd        doc;
  594.     
  595.     SetPort(window);
  596.         
  597.     doc = (DocHnd)GetWRefCon(window);
  598.  
  599.     if(doc != nil && GetIsAppWindow(window)) 
  600.     {
  601.         becomingActive = *(Boolean *)refCon;
  602.         if (becomingActive) 
  603.         {
  604.             DrawGrowIcon(window);
  605.             ShowControl((**doc).hScroll);
  606.             ShowControl((**doc).vScroll);
  607.         }
  608.         else 
  609.         {
  610.             HideControl((**doc).hScroll);
  611.             HideControl((**doc).vScroll);
  612.             DrawGrowIcon(window);
  613.         }
  614.         
  615.     }
  616.     
  617. }